package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.ClearMaterielListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotList;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.SimpleEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: FTClearMaterielActivity
 * Author: XJH
 * Date: 2020/5/22 10:54
 * Description:FT测试，清料
 * History:
 */
public class FTClearMaterielActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
//    @BindView(R.id.etLot)
//    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpMainWork)
    FreeUI_ClearEditText etEqpMainWork;
    @BindView(R.id.tvEqpItemWork)
    TextView tvEqpItemWork;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.rvClearMaterielList)
    RecyclerView rvClearMaterielList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llLotList)
    LinearLayout llLotList;
    @BindView(R.id.llLot)
    LinearLayout llLot;
    @BindView(R.id.ivArrow)
    ImageView ivArrow;
    @BindView(R.id.tvCode)
    TextView tvCode;
    @BindView(R.id.flList)
    FrameLayout flList;
    private ClearMaterielListAdapter mClearMaterielListAdapter;
    private ArrayList<String> lotList = new ArrayList<>();
    private FreeUI_GeneralPop codePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_clear_materiel_ft;
    }

    @Override
    protected void initView() {
        initLayout();
        llLotList.setVisibility(View.VISIBLE);
        llLot.setVisibility(View.GONE);
//        initEdit(etLot, s -> LotUtils.searchLotInfo(FTClearMaterielActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
//            @Override
//            public void onSuccess(LotInfoEntity response) {
//                tvNum.setText(response.getLOT().getMAINQTY());
//                etEqpMainWork.setText(response.getLOT().getEQUIPMENTID());
//                requestData(getText(etLot),getText(etEqpMainWork));
//            }
//
//            @Override
//            public void onFail(Throwable t) {
//
//            }
//        }));
        initEdit(etEqpMainWork,s ->{
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    ("正在查询LOT信息", new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(FTClearMaterielActivity.this, true, getSupportFragmentManager())) {
                        if (response.body().getResponse().getBody().getLOTLIST().size() > 0){
                            for (LotInfoEntity.LotBean s:response.body().getResponse().getBody().getLOTLIST()) {
                                lotList.add(s.getLOTID());
                                if (lotList.size() == 1){
                                    tvCode.setText(lotList.get(0));
                                    requestData(lotList.get(0),getText(etEqpMainWork));
                                }
                            }
                        }else {
                            showToast("未查询到批号信息",false);
                        }
                    }
                }
                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);

                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "LOTLISTRUNBYEQP");
            map.addProperty("LOTID", "");
            map.addProperty("LOTALIAS", "");
            map.addProperty("DURABLE", "");
            map.addProperty("EQUIPMENTID", s);
            map.addProperty("STEPNAME", "");
            NetUtils.requestPostNet("IF.LOTQUERY", "Common", FTClearMaterielActivity.this, map, callBack);
//
//            requestData(getText(etLot),s);
        });
        flList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flList.getWidth(), lotList, ivArrow, (codeS, i) -> {
                        tvCode.setText(codeS);
                        requestData(codeS,getText(etEqpMainWork));
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivArrow);
                codePop.showAsDropDown(flList, 0, 0);
            }
        });
    }

    private void requestData(String lotID,String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    SimpleEntity entity = response.body().getResponse().getBody();
                    tvEqpItemWork.setText(entity.getLISTEQUIPMENT().size()==0?"":entity.getLISTEQUIPMENT().get(0).getEQUIPMENTID());
                    if (!entity.getTRACKSHOWMSG().equals("")){
                        showConfirmDialog(entity.getTRACKSHOWMSG());
                    }
                    if (entity.getLOTBINDATALIST().size() <= 0){
                        showToast("暂无BIN数据",false);
                        return;
                    }

                    mClearMaterielListAdapter.setNewData(entity.getLOTBINDATALIST());
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKOUT");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "SelectClearData");
        map.addProperty("EQUIPMENTID", eqpID);
        List<LotList> lotList = new ArrayList<>();
        lotList.add(new LotList(lotID));
        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
        NetUtils.requestPostNet("IF.LOTFTTRACKOUT", "LOTFTTRACKOUT", FTClearMaterielActivity.this, map, callBack);
    }

    private void initLayout() {
        listTv1.setText("批号");
        listTv2.setText("BIN名称");
        listTv3.setText("数量");
        listTv4.setText("动作类型");
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        initRecyclerView(rvClearMaterielList,this);
        setMainLayout(llMain);
        mClearMaterielListAdapter = new ClearMaterielListAdapter(null);
        mClearMaterielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mClearMaterielListAdapter.isFirstOnly(true);
        rvClearMaterielList.setAdapter(mClearMaterielListAdapter);

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked() {
//        if (actionControl("Submit04"))return;
        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {
                FreeUI_EntityCallBack<BaseResponseBean<SimpleEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<SimpleEntity>>
                        (new TypeToken<BaseResponseBean<SimpleEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<SimpleEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            String trackshowmsg = response.body().getResponse().getBody().getTRACKSHOWMSG();
                            showConfirmDialog(FTClearMaterielActivity.this,trackshowmsg.equals("") ? "清料成功" : trackshowmsg);
                        }


                    }

                    @Override
                    public void onError(Response<BaseResponseBean<SimpleEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "LOTFTTRACKOUT");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","ClearTrackout");
                map.addProperty("EQUIPMENTID",getText(etEqpMainWork));
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(tvCode.getText().toString()));
                map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
                List<SimpleEntity.MantissaLotBinData> adapterData = mClearMaterielListAdapter.getData();
                for (SimpleEntity.MantissaLotBinData data : mClearMaterielListAdapter.getData()){
                    switch (data.getCLEARTYPE()){
                        case "留站内":
                            data.setCLEARTYPE("None");
                            break;
                        case "返工":
                            data.setCLEARTYPE("Rework");
                            break;
                        case "报废":
                            data.setCLEARTYPE("Scrap");
                            break;
                        case "分批":
                            data.setCLEARTYPE("Split");
                            break;
                    }
                }

                map.add("LOTBINDATALIST",new JsonParser().parse(new Gson().toJson(adapterData)).getAsJsonArray());
                NetUtils.requestPostNet("IF.LOTFTTRACKOUT","LOTFTTRACKOUT",FTClearMaterielActivity.this,map,callBack);
            }
        });
    }

}
